package com.xsm.activitygroup;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.xsm.activitygroup.R;
import com.xsm.activitygroup.BookMessage.LoadMessage;

import android.R.integer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
public class PhotoView extends Activity{
	ImageView imageView;
   String path="";
	Bitmap bitmap;
	private static int  REFRESHING=0;
	ProgressBar progressBar;
protected void onCreate(Bundle savedInstanceState) {
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	super.onCreate(savedInstanceState);
	setContentView(R.layout.photoview);
	path=getIntent().getExtras().getString("path");
	imageView=(ImageView)findViewById(R.id.photoview);
	progressBar=(ProgressBar)findViewById(R.id.pro);
imageView.setOnClickListener(new OnClickListener() {
	public void onClick(View v) {
	progressBar.setVisibility(View.VISIBLE);	
	new load().execute();
	}
});
	new load().execute();
}

public void Back(View view){
	finish();
}

public static Bitmap loadImageFromUrl(String url)
{

    URL m;
    InputStream i = null;
    BufferedInputStream bis = null;
    ByteArrayOutputStream out = null;
byte[] isBuffer=new byte[1000];
    if (url == null)
        return null;
    try
    {
        m = new URL(url);
        i = (InputStream) m.getContent();
        bis = new BufferedInputStream(i, 1024 * 4);
        out = new ByteArrayOutputStream();
        int len = 0;

        while ((len = bis.read(isBuffer)) != -1)
        {
            out.write(isBuffer, 0, len);
        }
        out.close();
        bis.close();
    } catch (MalformedURLException e1)
    {
        e1.printStackTrace();
        return null;
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    if (out == null)
        return null;
    byte[] data = out.toByteArray();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    Bitmap bmp =null;
    try
    {
        bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); //返回缩略图
    } catch (OutOfMemoryError e)
    {
        System.gc();
        bmp =null;
    }
    return bmp;
}

class load extends AsyncTask<Void, Integer, String>{
	protected String doInBackground(Void... params) {
		REFRESHING=1;
	    URL m;
	    InputStream i = null;
	    BufferedInputStream bis = null;
	    ByteArrayOutputStream out = null;
	    
	byte[] isBuffer=new byte[1000];
	    if (path == null)
	        return null;
	    try
	    {
	        m = new URL(path);
	        
	        HttpURLConnection   urlcon=(HttpURLConnection)m.openConnection();   
            //根据响应获取文件大小 
            int  fileLength=urlcon.getContentLength();
          progressBar.setMax(fileLength);
            
	        i = (InputStream) m.getContent();
	        bis = new BufferedInputStream(i, 1024 * 4);
	        out = new ByteArrayOutputStream();
	        
	        int len = 0;
              int count=0;
	        while ((len = bis.read(isBuffer)) != -1)
	        {
	            out.write(isBuffer, 0, len);
	            count+=len;
	            publishProgress(count);
	        }
	        out.close();
	        bis.close();
	    } catch (MalformedURLException e1)
	    {
	        e1.printStackTrace();
	        return null;
	    } catch (IOException e)
	    {
	        e.printStackTrace();
	    }
	    if (out == null)
	        return null;
	    byte[] data = out.toByteArray();
	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = false;
	    try
	    {
	        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options); //返回缩略图
	    } catch (OutOfMemoryError e)
	    {
	        System.gc();
	        bitmap =null;
	    }

	    
		return null;
	}
	
	protected void onPostExecute(String result) {
		REFRESHING=0;
		imageView.setImageBitmap(bitmap);
		progressBar.setVisibility(View.GONE);
		super.onPostExecute(result);
	}
	protected void onProgressUpdate(Integer... values) {
		progressBar.setProgress(values[0]);
		super.onProgressUpdate(values);
		}
}

}
